package com.qiantu.phone.ui.activity;

import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.fragment.SceneFragment;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class OnKeySceneSelectActivity extends AppActivity {

    private android.widget.FrameLayout sceneFragmentContainer;
    SceneFragment sceneFragment;
    private String serialNo;
    private LinkageBean mLinkageBean;
    private LinkageBean.LinkageTask mLinkageTask;
    private List<LinkageBean.LinkageTask.Scene> sceneList;
    private int mPosition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_manage;
    }

    @Override
    protected void initView() {
        mPosition = getInt("position", -1);
        sceneFragmentContainer = findViewById(R.id.scene_fragment_container);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        sceneFragment = SceneFragment.newInstance(true, true, SceneFragment.SELECT_SCENE);
        sceneFragment.showMaxSceneHint();
        transaction.add(R.id.scene_fragment_container, sceneFragment);
        transaction.commit();
        setTitle(R.string.one_key_scene);
        getTitleBar().setRightTitle(R.string.save);
        getTitleBar().getRightView().setEnabled(false);
    }

    @Override
    protected void initData() {
        serialNo = getString("serial_no");
        mLinkageBean = LinkageDataHolder.getLinkageData();
        if (mLinkageBean != null) {
            List<LinkageBean.LinkageTask> linkageTasks = mLinkageBean.getLinkageTasks();
            if (linkageTasks != null && linkageTasks.size() > 0 && !TextUtils.isEmpty(serialNo)) {
                for (LinkageBean.LinkageTask item : linkageTasks) {
                    if (!TextUtils.isEmpty(item.getTaskSerialNo()) && item.getTaskSerialNo().equals(serialNo)) {
                        mLinkageTask = item;
                        sceneList = mLinkageTask.getSceneList();
                        break;
                    }
                }
            }
        }
        updateView();
    }

    private void updateView() {
        if(sceneList != null && sceneList.size() > 0){
            List<String> sceneSerialNos = new ArrayList<>();
            for(LinkageBean.LinkageTask.Scene scene : sceneList){
                sceneSerialNos.add(scene.getSceneSerialNo());
            }
            sceneFragment.setSelectedScenes(sceneSerialNos);
            getTitleBar().getRightView().setEnabled(true);
        }else{
            getTitleBar().getRightView().setEnabled(false);
        }
    }

    @Override
    public void onRightClick(View view) {
        List<SceneBean> sceneBeans = sceneFragment.getSelectedScenes();
        List<LinkageBean.LinkageTask.Scene> serialNos = new ArrayList<>();
        for(SceneBean scene : sceneBeans){
            LinkageBean.LinkageTask.Scene oneKeyScene = new LinkageBean.LinkageTask.Scene();
            oneKeyScene.setSceneSerialNo(scene.getSceneSerialNo());
            serialNos.add(oneKeyScene);
        }
        sceneList = serialNos;
        //当前联动空，才去
        if (mLinkageTask == null) {
            //本地保存联动条件
            if (mLinkageBean == null) {
                mLinkageBean = new LinkageBean();
                LinkageDataHolder.setLinkageData(mLinkageBean);
            }
            List<LinkageBean.LinkageTask> linkageTasks = mLinkageBean.getLinkageTasks();
            if (linkageTasks == null) {
                linkageTasks = new ArrayList<>();
            }
            mLinkageBean.setLinkageTasks(linkageTasks);
            mLinkageTask = new LinkageBean.LinkageTask();
            //先设置个本地的SerialNo，为了编辑找到对应的数据
            mLinkageTask.setTaskSerialNo(UUID.randomUUID().toString());
            mLinkageTask.setTaskType(5);
            if(mPosition < 0){
                linkageTasks.add(mLinkageTask);
            }else{
                linkageTasks.add(mPosition, mLinkageTask);
            }
        }
        mLinkageTask.setSceneList(sceneList);
        mLinkageTask.setImageUrl("icon_intelligent_scenes");
        mLinkageTask.setBriefing("一键场景");
        startActivity_SingleTask(EditLinkageActivity.class);
        finish();
    }
}